package com.scaffold.myproject.service.mqproducer;

import com.scaffold.myproject.constants.MQConstants;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
@Slf4j
public class RocketMQProducer {

    private final RocketMQTemplate rocketMQTemplate;

    public void sendMessage(String msg) {
        try {
            rocketMQTemplate.syncSend(MQConstants.TOPIC_USER + ":" + MQConstants.TAG_USER
                    , MessageBuilder.withPayload(msg).build());
            log.info("发送消息成功，topic={}, tag={}, msg={}", MQConstants.TOPIC_USER, MQConstants.TAG_USER, msg);
        } catch (Exception e) {
            log.error("发送消息异常 topic={}, tag={}, msg={}", MQConstants.TOPIC_USER, MQConstants.TAG_USER, msg);
        }
    }
}
